package com.huike.web.controller;

import io.jsonwebtoken.*;
import org.aspectj.lang.Signature;

import java.util.Date;

/**
 * @author BaiYang
 * @time 2022/9/24
 */
public class TokenTest {
    public static void main(String[] args) {
        String signature = "11122223334445556667778889990";
        JwtBuilder builder = Jwts.builder();
        String token = builder.setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .claim("username", "zhangsan")
                .claim("name", "张三")
                .claim("age", 18)
                .claim("sex", "女")
                .setExpiration(new Date(System.currentTimeMillis() + Long.valueOf(1000 * 60 * 60 * 1)))
                .setId("112233441314")
                .signWith(SignatureAlgorithm.HS256, signature)
                .compact();
        System.out.println(token);

        JwtParser parser = Jwts.parser();
        Jws<Claims> claimsJws = parser.setSigningKey(signature).parseClaimsJws(token);
        Claims claims = claimsJws.getBody();
        System.out.println(claims.get("username"));
        System.out.println(claims.get("sex"));
        System.out.println(claims.get("age"));
        System.out.println(claims.get("name"));
        System.out.println(claims.getId());
        System.out.println(claims.getSubject());
        System.out.println(claims.getExpiration());
    }

}
